package com.glp.work.flow.workflow.plugin.flowable.controller;
//package com.glp.work.flow.controller;
//
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Set;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.apache.commons.lang.StringUtils;
//import org.flowable.bpmn.model.BpmnModel;
//import org.flowable.common.engine.impl.identity.Authentication;
//import org.flowable.engine.ProcessEngine;
//import org.flowable.engine.ProcessEngineConfiguration;
//import org.flowable.engine.RepositoryService;
//import org.flowable.engine.RuntimeService;
//import org.flowable.engine.TaskService;
//import org.flowable.engine.runtime.Execution;
//import org.flowable.engine.runtime.ProcessInstance;
//import org.flowable.image.ProcessDiagramGenerator;
//import org.flowable.task.api.Task;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.glp.common.reponse.BaseResponse;
//import com.glp.common.reponse.BaseResult;
//import com.glp.work.flow.pojo.FlowTaskDto;
//import com.glp.work.flow.workflow.plugin.flowable.dto.FlowDataListDto;
//import com.glp.work.flow.workflow.plugin.flowable.dto.FlowTaskHistoryDto;
//import com.glp.work.flow.workflow.plugin.flowable.dto.UserTaskExtensionDTO;
//import com.glp.work.flow.workflow.plugin.flowable.service.FlowProcessDefinitionService;
//import com.glp.work.flow.workflow.plugin.flowable.service.FlowTaskService;
//import com.google.common.collect.Sets;
//
//import io.lettuce.core.dynamic.annotation.Param;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//
///**
// * 工作流POC程序-任务
// *
// * @author Pan Wang
// * @date 2019年03月13日 10:51:50
// */
//@RestController
//@RequestMapping("/poc")
//@Api(tags = "工作流POC程序-任务")
//@Slf4j
//public class PocTaskController {
//    @Autowired
//    private RuntimeService runtimeService;
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private RepositoryService repositoryService;
//    @Autowired
//    private ProcessEngine processEngine;
//    @Autowired
//    private FlowTaskService flowTaskService;    
//    @Autowired
//    private FlowProcessDefinitionService flowProcessDefinitionService;
//    
//    /**
//     * 添加报销
//     *
//     * @param userId    用户Id
//     * @param money     报销金额
//     * @param descption 描述
//     */
//    @PostMapping("submit")
//    @ApiOperation("提交任务")
//    public String submit(String userId, Integer money, String descption) {
//        //启动流程
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("taskUser", userId);
//        map.put("money", money);
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Expense", map);
//        return "提交成功.流程Id为：" + processInstance.getId();
//    }
//
//    /**
//     * 获取审批管理列表
//     */
//    @RequestMapping(value = "/list")
//    @ResponseBody
//    public Object list(String userId) {
//        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
//        for (Task task : tasks) {
//            System.out.println(task.toString());
//        }
//        return tasks.toArray().toString();
//    }
//    
//    @RequestMapping(value = "todo")
//    @ResponseBody
//    public FlowDataListDto todoData( HttpServletRequest request) {
//    	FlowTaskDto flowTaskDto = new FlowTaskDto();
//    	flowTaskDto.setProcDefId("Expense");
//        //联查变量的情况下，分页功能失效
//    	Set<String> codes = Sets.newHashSet();
//        return flowTaskService.queryTodoList(FlowDataListDto.get(request), flowTaskDto, "123", codes, false);
//    }    
//    @RequestMapping(value = "historicList")
//    @ResponseBody
//    public FlowDataListDto historicList( HttpServletRequest request) {
//    	FlowTaskDto flowTaskDto = new FlowTaskDto();
//    	flowTaskDto.setProcDefId("Expense");
//        //联查变量的情况下，分页功能失效
//    	Set<String> codes = Sets.newHashSet();
//        return flowTaskService.historicList(FlowDataListDto.get(request), flowTaskDto, "123");
//    }    
//    
//	public FlowDataListDto todoData(@Param("::") FlowTaskDto flowTaskDto,String userName, HttpServletRequest request) {
//        return flowTaskService.queryTodoList(FlowDataListDto.get(request), flowTaskDto, userName
//        		, null, false);
//    }
//
//    public FlowDataListDto historicDataList(FlowTaskDto flowTaskDto, HttpServletRequest request,String userName, HttpServletResponse response) {
//        return flowTaskService.historicList(FlowDataListDto.get(request), flowTaskDto, userName);
//    }
//
//    public FlowDataListDto hasSentDataList(FlowTaskDto flowTaskDto, String userName, HttpServletRequest request, HttpServletResponse response) {
//        return flowTaskService.hasSentList(FlowDataListDto.get(request), userName);
//    }
//
//    public List<FlowTaskHistoryDto> histoicFlow(@Param("procInsId") String procInsId, @Param("startAct") String startAct, @Param("endAct") String endAct) {
//            return flowTaskService.histoicFlowList(procInsId, startAct, endAct);
//    }
//
//    public FlowDataListDto processDataList(String category, HttpServletRequest request) {
//        return flowProcessDefinitionService.processList(FlowDataListDto.get(request), category);
//    }
//
//
//    public UserTaskExtensionDTO getUserTaskExtension(String taskDefKey, String procDefId) {
//        //节点流程信息
//        UserTaskExtensionDTO dto = flowProcessDefinitionService.getUserTaskExtension(taskDefKey, procDefId);
//        if (dto == null) {
//            return UserTaskExtensionDTO.NEW();
//        }
//        return dto;
//    }
//
//    public BaseResult delegate(String taskId, String userName) {
//        if (StringUtils.isBlank(userName) || StringUtils.isBlank(taskId)) {
//            return BaseResponse.error("参数异常");
//        }
//        flowTaskService.delegateTask(taskId, userName);
//        return BaseResponse.success("委托成功");
//    }
//
//    public BaseResult unclaim(String taskId,String userName) {
//        flowTaskService.unclaim(taskId, userName);
//        return BaseResponse.success("取消签收成功");
//    }
//
//
//    public BaseResult transferTask(String taskId, String userName, String reason) {
//        if (StringUtils.isBlank(reason)) {
//            return BaseResponse.error("请输入转派原因");
//        }
//        if (StringUtils.isBlank(userName) || StringUtils.isBlank(taskId)) {
//            return BaseResponse.error("参数异常");
//        }
//        // 设置当前流程任务办理人
//        Authentication.setAuthenticatedUserId(userName);
//        flowTaskService.transferTask(taskId, userName, reason);
//        return BaseResponse.success("委托成功");
//    }
//
//    public BaseResult deleteTask(String taskId, String reason) {
//        if (StringUtils.isBlank(reason)) {
//            return BaseResponse.error("删除失败，请填写删除原因！");
//        } else {
//            flowTaskService.deleteTask(taskId, reason);
//        }
//        return BaseResponse.success("删除任务成功，任务ID=" + taskId);
//    }
//    
//
//    /**
//     * 批准
//     *
//     * @param taskId 任务ID
//     */
//    @RequestMapping(value = "apply")
//    @ResponseBody
//    public String apply(String taskId) {
//        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        if (task == null) {
//            throw new RuntimeException("流程不存在");
//        }
//        //通过审核
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("outcome", "通过");
//        taskService.complete(taskId, map);
//        return "processed ok!";
//    }
//
//    /**
//     * 拒绝
//     */
//    @ResponseBody
//    @RequestMapping(value = "reject")
//    public String reject(String taskId) {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("outcome", "驳回");
//        taskService.complete(taskId, map);
//        return "reject";
//    }
//
//    /**
//     * 生成流程图
//     *
//     * @param processId 任务ID
//     */
//    @RequestMapping(value = "processDiagram")
//    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
//        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//
//        //流程走完的不显示图
//        if (pi == null) {
//            return;
//        }
//        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
//        //使用流程实例ID，查询正在执行的执行对象表，返回流程实例对象
//        String InstanceId = task.getProcessInstanceId();
//        List<Execution> executions = runtimeService
//                .createExecutionQuery()
//                .processInstanceId(InstanceId)
//                .list();
//
//        //得到正在执行的Activity的Id
//        List<String> activityIds = new ArrayList<>();
//        List<String> flows = new ArrayList<>();
//        for (Execution exe : executions) {
//            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
//            activityIds.addAll(ids);
//        }
//
//        //获取流程图
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
//        ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
//        ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
//        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png",  engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), false);
//        OutputStream out = null;
//        byte[] buf = new byte[1024];
//        int legth = 0;
//        try {
//            out = httpServletResponse.getOutputStream();
//            while ((legth = in.read(buf)) != -1) {
//                out.write(buf, 0, legth);
//            }
//        } finally {
//            if (in != null) {
//                in.close();
//            }
//            if (out != null) {
//                out.close();
//            }
//        }
//    }
//}
